package com.joker.demo.admin.controller;

import com.joker.demo.admin.common.Result;
import com.joker.demo.admin.service.smart.JobTriggerHandleHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 定时任务触发控制器
 *
 * @author : feixiang.li
 * @since : 2025-01-27
 */
@Slf4j
@RestController
@RequestMapping("/api/job-trigger")
@Tag(name = "定时任务触发管理", description = "定时任务触发处理器的开启、关闭和状态查询")
public class JobTriggerController {

    @Resource
    private JobTriggerHandleHelper jobTriggerHandleHelper;

    @PostMapping("/start")
    @Operation(summary = "开启定时任务触发处理器", description = "启动定时任务触发处理器，开始自动处理待执行的任务")
    public Result<String> startJobTrigger() {
        try {
            log.info("收到开启定时任务触发处理器的请求");
            jobTriggerHandleHelper.startJobTrigger();
            return Result.success("定时任务触发处理器已成功开启");
        } catch (Exception e) {
            log.error("开启定时任务触发处理器失败", e);
            return Result.error("开启定时任务触发处理器失败: " + e.getMessage());
        }
    }

    @PostMapping("/stop")
    @Operation(summary = "关闭定时任务触发处理器", description = "停止定时任务触发处理器，停止自动处理待执行的任务")
    public Result<String> stopJobTrigger() {
        try {
            log.info("收到关闭定时任务触发处理器的请求");
            jobTriggerHandleHelper.stopJobTrigger();
            return Result.success("定时任务触发处理器已成功关闭");
        } catch (Exception e) {
            log.error("关闭定时任务触发处理器失败", e);
            return Result.error("关闭定时任务触发处理器失败: " + e.getMessage());
        }
    }

    @GetMapping("/status")
    @Operation(summary = "查询定时任务触发处理器状态", description = "获取定时任务触发处理器的当前运行状态")
    public Result<JobTriggerStatusVO> getJobTriggerStatus() {
        try {
            log.info("收到查询定时任务触发处理器状态的请求");
            JobTriggerStatusVO status = jobTriggerHandleHelper.getJobTriggerStatus();
            return Result.success(status);
        } catch (Exception e) {
            log.error("查询定时任务触发处理器状态失败", e);
            return Result.error("查询定时任务触发处理器状态失败: " + e.getMessage());
        }
    }

    /**
     * 定时任务触发处理器状态VO
     */
    public static class JobTriggerStatusVO {
        /**
         * 是否正在运行
         */
        private Boolean running;

        /**
         * 状态描述
         */
        private String statusDescription;

        /**
         * 启动时间
         */
        private String startTime;

        /**
         * 最后更新时间
         */
        private String lastUpdateTime;

        public JobTriggerStatusVO() {
        }

        public JobTriggerStatusVO(Boolean running, String statusDescription, String startTime, String lastUpdateTime) {
            this.running = running;
            this.statusDescription = statusDescription;
            this.startTime = startTime;
            this.lastUpdateTime = lastUpdateTime;
        }

        // Getters and Setters
        public Boolean getRunning() {
            return running;
        }

        public void setRunning(Boolean running) {
            this.running = running;
        }

        public String getStatusDescription() {
            return statusDescription;
        }

        public void setStatusDescription(String statusDescription) {
            this.statusDescription = statusDescription;
        }

        public String getStartTime() {
            return startTime;
        }

        public void setStartTime(String startTime) {
            this.startTime = startTime;
        }

        public String getLastUpdateTime() {
            return lastUpdateTime;
        }

        public void setLastUpdateTime(String lastUpdateTime) {
            this.lastUpdateTime = lastUpdateTime;
        }
    }
}
